# coding=utf-8
# 上市股东信息实体解析


from i_entity_extractor.extractors.default.default_extractor import DefaultExtractor
from i_entity_extractor.common_parser_lib import toolsutil
import pandas
import copy

class SsgsShareholderExtractor(DefaultExtractor):
    def __init__(self, topic_info, log):
        DefaultExtractor.__init__(self, topic_info, log)
        self.shareholders_map_conf = {
            'nan': "year_quarter",
            u"股东人数(户)": "shareholder_number",
            u"较上期变化(%)": "shareholder_number_compared_previous_change",
            u"人均流通股(股)": "per_capita_float",
            u"人均流通股较上期变化": "per_capita_float_compared_previous_change",
            u"筹码集中度": "chip_concentration",
            u"股价(元)": "share_price",
            u"人均持股金额(元)": "capita_holdings_amount_pre",
            u"前十大股东持股合计(%)": "top10_shareholders_aggregate",
            u"前十大流通股东持股合计(%)": "top10_holdings_total_circulation",
        }


    def entity_extract(self, parse_info, extract_data):
        '''上市股东信息实体解析入口'''
        extract_info = parse_info.extract_info
        url = parse_info.base_info.url
        self.log.info("ssgs_shareholder_start_parse\turl:%s\ttopic_id:%s" % (url, extract_info.topic_id))
        entity_data = self.format_extract_data(extract_data)

        return entity_data

    def format_extract_data(self, extract_data):
        '''实体解析抽取数据'''
        entity_data = copy.deepcopy(extract_data)

        top10_shareholders_info     = extract_data.get("top10_shareholders_info")
        top10_shareholders_year     = extract_data.get("top10_shareholders_year")
        ten_outstanding_shares_info = extract_data.get("ten_outstanding_shares_info")
        ten_outstanding_shares_year = extract_data.get("ten_outstanding_shares_year")

        fund_table_list = []
        if entity_data.has_key("fund_table_info") and entity_data.has_key("fund_table_year"):
            fund_table_info = extract_data.get("fund_table_info")
            fund_table_year = extract_data.get("fund_table_year")
            if len(fund_table_info) == len(fund_table_year):
                num = len(fund_table_info)
                for index in range(num):
                    for key in fund_table_year[index].keys():
                        fund_info = fund_table_info[index].get('fund_info')
                        if isinstance(fund_info, list):
                            for index_num in range(len(fund_info)):
                                if key not in fund_info[index_num].keys():
                                    fund_info[index_num][key] = top10_shareholders_year[index][key]
                    for item in fund_info:
                        fund_table_list.append(item)
            else:
                self.log.error(
                    "fund_table_info_len not euqal fund_table_year_len, fund_table_info_len:%s\tfund_table_year_len:%s" % (
                    len(fund_table_info), len(fund_table_year)))

        top10_shareholders_list = []
        if entity_data.has_key("top10_shareholders_info") and entity_data.has_key("top10_shareholders_year"):
            if len(top10_shareholders_info) == len(top10_shareholders_year):
                num = len(top10_shareholders_info)
                for index in range(num):
                    for key in top10_shareholders_year[index].keys():
                        shareholders = top10_shareholders_info[index].get('shareholders')
                        if isinstance(shareholders, list):
                            for index_num in range(len(shareholders)):
                                if key not in shareholders[index_num].keys():
                                    shareholders[index_num][key] = top10_shareholders_year[index][key]
                    for item in shareholders:
                        top10_shareholders_list.append(item)
            else:
                self.log.error(
                    "top10_shareholders_info_len not euqal top10_shareholders_year_len, top10_shareholders_info_len:%s\ttop10_shareholders_year_len:%s" % (
                    len(top10_shareholders_info), len(top10_shareholders_year)))

        ten_outstanding_shares_list = []
        if entity_data.has_key("top10_shareholders_info") and entity_data.has_key("ten_outstanding_shares_year"):
            if len(ten_outstanding_shares_info) == len(ten_outstanding_shares_year):
                num = len(ten_outstanding_shares_info)
                for index in range(num):
                    for key in ten_outstanding_shares_year[index].keys():
                        shareholders = ten_outstanding_shares_info[index].get('shareholders')
                        if isinstance(shareholders, list):
                            for index_num in range(len(shareholders)):
                                if key not in shareholders[index_num].keys():
                                    shareholders[index_num][key] = top10_shareholders_year[index][key]
                    for item in shareholders:
                        ten_outstanding_shares_list.append(item)
            else:
                self.log.error(
                    "ten_outstanding_shares_info_len not euqal ten_outstanding_shares_year_len, ten_outstanding_shares_info_len:%s\tten_outstanding_shares_year_len:%s" % (
                    len(ten_outstanding_shares_info), len(ten_outstanding_shares_year)))

        code = extract_data.get('code')
        stock_code = None
        if code:
            stock_code = toolsutil.re_find_one('\d+', extract_data.get('code'))

        shareholder_number_table = extract_data.get('shareholder_number_table')
        shareholder_number_list = []

        if isinstance(shareholder_number_table, basestring):
            net_table_data = toolsutil.html2excel(shareholder_number_table)
            if isinstance(net_table_data, pandas.core.frame.DataFrame):
                net_table_data = net_table_data.T.values
                head_keys = net_table_data[0]
                for index in range(len(net_table_data)):
                    if index == 0:
                        continue
                    tmp_map = {}
                    for index_num in range(len(net_table_data[index])):
                        key = head_keys[index_num]
                        tmp_map[key] = net_table_data[index][index_num]
                        shareholder_number_list.append(tmp_map)

        shareholder_number_list = self.format_shareholder_number_info(shareholder_number_list)

        entity_data["ten_outstanding_shares"] = ten_outstanding_shares_list
        entity_data["top10_shareholders"] = top10_shareholders_list
        entity_data["fund_table"] = fund_table_list
        entity_data["code"] = code
        entity_data["shareholder_number_table"] = shareholder_number_list
        entity_data["stock_code"] = stock_code
        return entity_data

    def format_shareholder_number_info(self, shareholder_number_list):
        '''格式化shareholder_number_list'''
        result_list = []
        for item in shareholder_number_list:
            tmp_map = {}
            for key, value in item.items():
                if isinstance(key, float):
                    key = str(key)
                tmp_map[self.shareholders_map_conf.get(key, '')] = value
            result_list.append(tmp_map)
        return result_list




if __name__ == "__main__":
    extract_data = {}


    import sys
    import json

    sys.path.append('../../')
    topic_id = 123

    import pytoml
    from conf import get_config
    from bdp.i_crawler.i_extractor.ttypes import BaseInfo, CrawlInfo, ExtractInfo, PageParseInfo

    with open('../../entity.toml', 'rb') as config:
        config = pytoml.load(config)
    conf2 = get_config(config)

    import common
    from i_entity_extractor.entity_extractor_route import entity_route_obj

    topic_info = entity_route_obj.read_topics()[topic_id]

    obj = SsgsShareholderExtractor(topic_info, common.log)

    src_url = "www.baidu.com"
    data = json.dumps(extract_data)
    extract_info = ExtractInfo(ex_status=2, extract_data=data)
    base_info = BaseInfo(url=src_url)
    parser_info = PageParseInfo(base_info=base_info, extract_info=extract_info)
    data = obj.entity_extract(parser_info, extract_data)
    print data

    for key, value in data.items():
        if isinstance(value, list):
            for i in value:
                print key, ":", i
        elif isinstance(value, dict):
            for key2, value2 in value.items():
                print key2, ":", value2
        else:
            print key, ":", value
